/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame.Units;

import com.jme3.asset.AssetManager;
import com.jme3.bounding.BoundingSphere;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.shapes.HullCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.util.TangentBinormalGenerator;
import mygame.Units.attacks.Cannon;
import mygame.Units.attacks.Weapon;
import mygame.World;

/**
 *
 * @author Ricardo
 */
public class EnemyBallon extends Enemy{
    static int counter=0;
    
    RigidBodyControl rigidBody;
    Vector3f location=new Vector3f();
    Quaternion rotation = new Quaternion();
    
    
    
    
    public EnemyBallon() {
        super(counter+"EnemyBallon", 100, 1, 1, 0);
        counter++;
    }
    
    

    @Override
    public void build(AssetManager assetManager, PhysicsSpace physicsSpace, Node rootNode, Node shootables) {
        Spatial geom = assetManager.loadModel("Models/boat.j3o");
        geom.setName(name);
        TangentBinormalGenerator.generate(geom);           // for lighting effect
        Material mat_default = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
        mat_default.setTexture("DiffuseMap", assetManager.loadTexture("Textures/ballon.jpg"));
        mat_default.setTexture("NormalMap", assetManager.loadTexture("Textures/ballon_normal.jpg"));
        mat_default.setBoolean("UseMaterialColors",true);
//        mat_default.setBoolean("VertexLighting", true );
        mat_default.setColor("Specular",ColorRGBA.White);
        mat_default.setColor("Diffuse",ColorRGBA.Red);
        mat_default.setFloat("Shininess", 5f); // [1,128]    
        geom.setMaterial(mat_default);
        shootables.attachChild(geom);
        BoundingSphere bs=new BoundingSphere();
        ((Geometry)geom).getMesh().setBound(bs);
        ((Geometry)geom).getMesh().updateBound();
        
        rigidBody = new RigidBodyControl( new HullCollisionShape(((Geometry) geom).getMesh()) , 0.1f ); // dynamic
        geom.addControl(rigidBody);
        physicsSpace.add(rigidBody); 
        //drag
        rigidBody.setDamping(0.5f, 0.5f);
        
        //weapons
        Cannon cannon=new Cannon();
        cannon.build(assetManager, physicsSpace, rootNode, shootables);
        cannon.attach(this, new Vector3f(0, -3, 0), true, true, false);
        waepons = new Weapon[1];
        waepons[0]=cannon;
    }
    
    
    @Override
    public void update(float tpf) {
        //update location
        rigidBody.getPhysicsLocation(location);
        rigidBody.getPhysicsRotation(rotation);
        //hot air
        rigidBody.applyCentralForce(Vector3f.UNIT_Y);
        //center of mass
        Vector3f ndown = new Vector3f(0, -1, 0);
        Vector3f nforward = new Vector3f(1, 0, 0);
        rigidBody.getPhysicsRotation().multLocal(ndown);
        rigidBody.getPhysicsRotation().multLocal(nforward);
        ndown.subtractLocal(VECTOR_DOWN);
        if(ndown.y>0)ndown.y=0;
        nforward.subtractLocal(VECTOR_FORWARD);
        rigidBody.applyForce(ndown, new Vector3f(0, 1, 0));
        rigidBody.applyForce(nforward, new Vector3f(-1, 0, 0));
        //on rails 2,5D, lock Z
        rigidBody.setPhysicsLocation(rigidBody.getPhysicsLocation().multLocal(1, 1, 0));
        //on rails 2,5D, lock max Y
        Vector3f v=rigidBody.getPhysicsLocation();
        if(v.y>15){
            v.setY(15);
            rigidBody.setPhysicsLocation(v);
        }
        
        //follow player
        if(World.player.location.distanceSquared(location)>180)
        rigidBody.applyCentralForce(World.player.location.subtract(location).normalizeLocal());
        
        //fire ze missiles
        for (int i = 0; i < waepons.length; i++) {
            waepons[i].update(tpf);
        }
    }

    @Override
    public Vector3f getLocation() {
        return location;
    }
    @Override
    public void setLocation(Vector3f position){
        rigidBody.setPhysicsLocation(position);
    }

    @Override
    public Quaternion getRotation() {
        return rotation;
    }
}
